package _05处理流;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ObjectStreamDemo {

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException 
	{
		
//		testWrite();
		
		
		testRead();
		
	}

	private static void testRead() throws IOException, ClassNotFoundException 
	{
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("d:/stu.data"));
		
		/*Student stu = (Student) in.readObject();
		System.out.println(stu);*/
		List<Student> stus=(List<Student>)in.readObject();
		System.out.println(stus);
		
		in.close();
		
	}

	private static void testWrite() throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		
		// 1 打开流
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/stu.data"));
		
		
		// 2. 写入对象
		List<Student> stus = new ArrayList<>();
		stus.add(new Student(1,"张三",18));
		stus.add(new Student(2,"刘备",18));
		stus.add(new Student(3,"张飞",20));
		out.writeObject(stus);
		
		// 3. 关闭文件
		out.close();
		
	}
}


//Exception in thread "main" java.io.NotSerializableException: _05处理流.Student
//at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1192)
//at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:352)
//at _05处理流.ObjectStreamDemo.testWrite(ObjectStreamDemo.java:37)
//at _05处理流.ObjectStreamDemo.main(ObjectStreamDemo.java:13)

// Student要支持序列化
